<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增快捷</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">快捷标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="shortcut_title" value="" placeholder="请输入快捷标题" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">快捷图片</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="shortcut_icon" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="shortcut_icon"  permission="app.admin.upload.image">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-shortcut_icon"  permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" value="0" placeholder="请输入排序，排序越大越在前" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">快捷类型</label>
                        <div class="layui-input-block">
                            <div name="shortcut_type" id="shortcut_type" value="0" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item" style="display: none" id="shortcut_value_div">
                        <label class="layui-form-label">场景值</label>
                        <div class="layui-input-block">
                            <div name="shortcut_value" id="shortcut_value" value="" ></div>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const INSERT_API = "/app/admin/rl-shortcut/insert";
            const SELECT_GOODS_API = "/app/admin/rl-goods/selectGoodsTitles";
            const SELECT_GOODS_CATE_API = "/app/admin/rl-goods-cate/select?format=tree";
            //请求配套数据
            function getData(url,data){
                var reqData;
                layui.use(["form", "jquery"], function() {
                    let loading = layer.load();
                    layui.$.ajax({
                        url: url,
                        data: data,
                        async: false,
                        dataType: "json",
                        type: "post",
                        success: function(res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure('初始化数据失败');
                            }
                            reqData= res.data;
                        }
                    })
                });
                return reqData;
            }

            // 字段 快捷图片 shortcut_icon
            layui.use(["upload", "layer"], function() {
                let input = layui.$("#shortcut_icon").prev();
                input.prev().attr("src", input.val());
                layui.$("#attachment-choose-shortcut_icon").on("click", function() {
                    parent.layer.open({
                        type: 2,
                        title: "选择附件",
                        content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                        area: ["95%", "90%"],
                        success: function (layero, index) {
                            parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                input.val(data.url).prev().attr("src", data.url);
                            });
                        }
                    });
                });
                layui.upload.render({
                    elem: "#shortcut_icon",
                    value: "",
                    acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                    url: "/app/admin/upload/image",
                    field: "__file__",
                    done: function (res) {
                        if (res.code > 0) return layui.layer.msg(res.msg);
                        this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                    }
                });
            });
            
            // 字段 快捷类型，0无，1商品详情，2分类 shortcut_type
            layui.use(["form", "jquery", "xmSelect"], function() {
                let value = layui.$("#shortcut_type").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#shortcut_type",
                    name: "shortcut_type",
                    initValue: initValue,
                    value: value,
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                    data: [{"value":1,"name":"商品"},{"value":2,"name":"分类"}],
                    //监听选中选项动作
                    on: function(data){
                        layui.$("#shortcut_value_div").show();
                        var bannerValueData;
                        switch (data.arr[0]['value']) {
                            case 1:
                                bannerValueData=getData(SELECT_GOODS_API,{});
                                // 字段 场景值 shortcut_value
                                layui.use(["jquery", "xmSelect"], function() {
                                    let value = layui.$("#shortcut_value").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#shortcut_value",
                                        name: "shortcut_value",
                                        filterable: true,
                                        initValue: initValue,
                                        value: "",
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        data: bannerValueData,
                                        prop: {
                                            name: 'goods_title', // 自定义表示显示文本的属性名称
                                            value: 'id' // 自定义表示值的属性名称
                                        },
                                    })
                                });
                                break;
                            case 2:
                                bannerValueData=getData(SELECT_GOODS_CATE_API,{});
                                layui.xmSelect.render({
                                    el: "#shortcut_value",
                                    name: "shortcut_value",
                                    filterable: true,
                                    initValue: initValue,
                                    value: "0",
                                    model: {"icon":"hidden","label":{"type":"text"}},
                                    clickClose: true,
                                    radio: true,
                                    tree: {show:true,strict:false,clickCheck:true,clickExpand:false,expandedKeys:initValue},
                                    prop: {
                                        name: 'cate_title', // 自定义表示显示文本的属性名称
                                        value: 'id' // 自定义表示值的属性名称
                                    },
                                    data: bannerValueData,
                                })
                                break;
                            default:
                                return layui.popup.failure('选择的类型无效');
                        }
                    }
                })
            });

            //提交事件
            layui.use(["form", "popup","jquery"], function () {
                layui.$('input[name="shortcut_type"]').on('DOMSubtreeModified', function() {
                    console.log(2222,this)
                });

                layui.form.on("submit(save)", function (data) {
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>
</html>
